<!DOCTYPE html>
<html>
<head>
    <title>页面管理</title>
    {% include 'system/common/header.html' %}
    <style>
        .layui-dropdown-menu {
            position: absolute;
            top: 100%;
            left: 0;
            z-index: 1000;
            display: none;
            min-width: 120px;
            padding: 5px 0;
            margin: 2px 0 0;
            background-color: #fff;
            border: 1px solid #e6e6e6;
            border-radius: 2px;
            box-shadow: 0 2px 4px rgba(0,0,0,.12);
        }
        
        .layui-dropdown-menu a {
            display: block;
            padding: 8px 15px;
            clear: both;
            font-weight: 400;
            line-height: 1.42857143;
            color: #333;
            white-space: nowrap;
            text-decoration: none;
        }
        
        .layui-dropdown-menu a:hover {
            background-color: #f5f5f5;
            color: #262626;
        }
        
        .layui-dropdown-menu i {
            margin-right: 5px;
        }
        
        .layui-btn-group {
            position: relative;
        }
        
        .toolbar-container {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .toolbar-container .layui-btn-group,
        .toolbar-container .layui-btn {
            height: 32px;
            line-height: 30px;
        }
        
        .toolbar-container .layui-btn-group .layui-btn {
            height: 32px;
            line-height: 30px;
        }
    </style>
</head>
<body class="ppress-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item" style="margin-bottom: unset;">
                <label class="layui-form-label">页面标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="" class="layui-input">
                </div>
                <button class="layui-btn layui-btn-md" lay-submit lay-filter="page-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="page-table" lay-filter="page-table"></table>
    </div>
</div>
</body>
{% include 'system/common/footer.html' %}

<script type="text/html" id="page-toolbar">
    <div class="toolbar-container">
        {% if authorize("cms:post:add") %}
            <div class="layui-btn-group layui-btn-sm">
                <button class="layui-btn layui-btn-primary" lay-event="add-wangeditor">
                    <i class="ppress-icon ppress-icon-add"></i>
                    新增页面
                </button>
                <button class="layui-btn layui-btn-primary layui-btn-dropdown" id="add-dropdown">
                    <i class="layui-icon layui-icon-down"></i>
                </button>
            </div>
            <div class="layui-dropdown-menu" id="add-dropdown-menu" style="display: none;">
                <!--
                <a href="javascript:;" lay-event="add-rich">
                    <i class="layui-icon layui-icon-edit"></i>
                    富文本
                </a>
                -->
                <a href="javascript:;" lay-event="add-wangeditor">
                    <i class="layui-icon layui-icon-picture"></i>
                    富文本
                </a>
                <a href="javascript:;" lay-event="add-markdown">
                    <i class="layui-icon layui-icon-code-circle"></i>
                    Markdown
                </a>
            </div>
        {% endif %}

        {% if authorize("cms:post:remove") %}
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchRemove">
                <i class="ppress-icon ppress-icon-ashbin"></i>
                删除
            </button>
        {% endif %}
    </div>
</script>

<script type="text/html" id="page-bar">
    {% if authorize("cms:post:edit") %}
        <button class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i
                class="layui-icon layui-icon-edit"> 编辑</i>
        </button>
    {% endif %}
    {% if authorize("cms:post:remove") %}
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove"><i
                class="layui-icon layui-icon-delete"> 删除</i>
        </button>
    {% endif %}
</script>

<script type="text/html" id="status-tpl">
{% raw %}
    {{# if(d.status === 'published') { }}
        <span class="layui-badge layui-bg-green">已发布</span>
    {{# } else if(d.status === 'draft') { }}
        <span class="layui-badge layui-bg-orange">草稿</span>
    {{# } else { }}
        <span class="layui-badge layui-bg-gray">{{d.status}}</span>
    {{# } }}
{% endraw %}
</script>

<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table
        let form = layui.form
        let $ = layui.jquery
        let popup = layui.popup

        let MODULE_PATH = '/admin/cms/page/'

        let cols = [
            [
                {% if authorize("cms:post:remove") %}
                    { type: 'checkbox' },
                {% endif %}
                {title: '编号', field: 'id', align: 'center', width: 80},
                {title: '页面标题', field: 'title', align: 'left', width: 300},
                {title: '状态', field: 'status', align: 'center', width: 100, templet: '#status-tpl'},
                {title: '页面摘要', field: 'excerpt', align: 'left'},
                {title: '操作', toolbar: '#page-bar', align: 'center', width: 240}
            ]
        ]

        table.render({
            elem: '#page-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#page-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        })

        table.on('tool(page-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj)
            } else if (obj.event === 'edit') {
                window.edit(obj)
            }
        })

        table.on('toolbar(page-table)', function (obj) {
            if (obj.event === 'add') {
                window.add()
            } else if (obj.event === 'add-rich') {
                window.addRich()
            } else if (obj.event === 'add-wangeditor') {
                window.addWangEditor()
            } else if (obj.event === 'add-markdown') {
                window.addMarkdown()
            } else if (obj.event === 'refresh') {
                window.refresh()
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj)
            }
        })
        
        // 下拉菜单事件处理
        $(document).on('click', '#add-dropdown', function(e) {
            e.preventDefault()
            e.stopPropagation()
            $('#add-dropdown-menu').toggle()
        })
        
        // 点击其他地方隐藏下拉菜单
        $(document).on('click', function(e) {
            if (!$(e.target).closest('.layui-btn-group').length) {
                $('#add-dropdown-menu').hide()
            }
        })

        form.on('submit(page-query)', function (data) {
            table.reload('page-table', {where: data.field})
            return false
        })

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增富文本页面',
                shade: 0.1,
                area: ['1200px', '600px'],
                content: MODULE_PATH + 'add?type=rich'
            })
        }
        
        window.addRich = function () {
            layer.open({
                type: 2,
                title: '新增富文本页面',
                shade: 0.1,
                area: ['1200px', '600px'],
                content: MODULE_PATH + 'add?type=rich'
            })
        }
        
        window.addWangEditor = function () {
            layer.open({
                type: 2,
                title: '新增富文本页面',
                shade: 0.1,
                area: ['1200px', '600px'],
                content: MODULE_PATH + 'add_wangeditor'
            })
        }
        
        window.addMarkdown = function () {
            layer.open({
                type: 2,
                title: '新增Markdown页面',
                shade: 0.1,
                area: ['1200px', '600px'],
                content: MODULE_PATH + 'add_markdown'
            })
        }

        window.edit = function (obj) {
            // 根据页面类型跳转到不同的编辑页面
            let articleType = obj.data['type'] || 'rich';
            let editUrl = '';
            let editTitle = '';
            
            if (articleType === 'markdown') {
                editUrl = MODULE_PATH + 'edit_markdown/' + obj.data['id'];
                editTitle = '修改Markdown页面';
            } else if (articleType === 'wangEditor') {
                editUrl = MODULE_PATH + 'edit_wangeditor/' + obj.data['id'];
                editTitle = '修改wangEditor页面';
            } else {
                editUrl = MODULE_PATH + 'edit/' + obj.data['id'];
                editTitle = '修改富文本页面';
            }
            
            layer.open({
                type: 2,
                title: editTitle,
                shade: 0.1,
                area: ['1200px', '600px'],
                content: editUrl
            })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该页面', {icon: 3, title: '提示'}, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'remove/' + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del()
                            })
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000})
                        }
                    }
                })
            })
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data
            if (data.length === 0) {
                layer.msg('未选中数据', {
                    icon: 3,
                    time: 1000
                })
                return false
            }
            var ids = []
            var hasCheck = table.checkStatus('page-table')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            layer.confirm('确定要删除选中页面', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'batchRemove',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('page-table')
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.refresh = function () {
            table.reload('page-table')
        }
    })
</script>
</html>